package org.dromara.yuanshidian.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.file.MimeTypeUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.vo.SysOssUploadVo;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.domain.vo.SysUserImportVo;
import org.dromara.system.service.ISysOssService;
import org.dromara.yuanshidian.domain.Ab01;
import org.dromara.yuanshidian.domain.bo.Ab01NewBo;
import org.dromara.yuanshidian.domain.bo.ExamineDataBo;
import org.dromara.yuanshidian.service.IAb01NewService;
import org.dromara.yuanshidian.service.IExamineDataService;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.yuanshidian.domain.vo.Ab01Vo;
import org.dromara.yuanshidian.domain.bo.Ab01Bo;
import org.dromara.yuanshidian.service.IAb01Service;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 原单位信息
 *
 * @author tzhongtop
 * @date 2025-01-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/yuanshidian/ab01")
public class Ab01Controller extends BaseController {

    private final IAb01Service ab01Service;

    private final IExamineDataService examineDataService;

    private final IAb01NewService ab01NewService;

    private final ISysOssService ossService;

    /**
     * 查询原单位信息列表
     */
    @SaCheckPermission("yuanshidian:ab01:list")
    @GetMapping("/list")
    public TableDataInfo<Ab01Vo> list(Ab01Bo bo, PageQuery pageQuery) {
        return ab01Service.queryPageList(bo, pageQuery);
    }

    /**
     * 查询单位名称
     */
    @SaCheckPermission("yuanshidian:ab01:dwnamelist")
    @GetMapping("/dwnamelist")
    public List<Ab01Vo> dwnamelist(Ab01Bo bo) {
        return ab01Service.queryList(bo);
    }

    /**
     * 导出原单位信息列表
     */
    @SaCheckPermission("yuanshidian:ab01:export")
    @Log(title = "原单位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(Ab01Bo ab01bo, HttpServletResponse response) {
        if(ab01bo.getSplitID() !=null && !ab01bo.getSplitID().isEmpty()){
            String[] splitID = ab01bo.getSplitID().split(",");
            List<Ab01Vo> list = ab01Service.exportAb01(ab01bo, splitID);
            ExcelUtil.exportExcel(list, "原单位信息", Ab01Vo.class, response);
        }else {
            List<Ab01Vo> list = ab01Service.queryList(ab01bo);
            ExcelUtil.exportExcel(list, "原单位信息", Ab01Vo.class, response);
        }

    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "单位信息模板", Ab01Vo.class, response);
    }

    /**
     * 获取原单位信息详细信息
     *
     * @param ID 主键
     */
    @SaCheckPermission("yuanshidian:ab01:query")
    @GetMapping("/{ID}")
    public R<Ab01Vo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long ID) {
        return R.ok(ab01Service.queryById(ID));
    }

    /**
     * 新增原单位信息
     */
    @SaCheckPermission("yuanshidian:ab01:add")
    @Log(title = "原单位信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody Ab01Bo bo) {
        return toAjax(ab01Service.insertByBo(bo));
    }

    /**
     * 修改原单位信息
     */
    @SaCheckPermission("yuanshidian:ab01:edit")
    @Log(title = "原单位信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody Ab01Bo bo) {
        return toAjax(ab01Service.updateByBo(bo));
    }


    /**
     * 删除原单位信息
     *
     * @param ID 主键串
     */
    @SaCheckPermission("yuanshidian:ab01:remove")
    @Log(title = "原单位信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ID}")
    public R<Void> remove(@NotNull(message = "主键不能为空") @PathVariable Long ID) {
        Ab01Bo ab01 = new Ab01Bo();
        ab01.setId(ID);
        ab01.setDelStatus(1L);
        return toAjax(ab01Service.updateByBo(ab01));
    }

    /**
     * 单位数据提交审核
     */
    @SaCheckPermission("yuanshidian:ab01:submit")
    @Log(title = "单位数据提交审核接口", businessType = BusinessType.UPDATE)
    @GetMapping("/submit/{ID}")
    public R<Void> submit(@NotNull(message = "主键不能为空") @PathVariable Long ID ) {
        Ab01Bo ab01Bo = new Ab01Bo();
        ab01Bo.setId(ID);
        ab01Bo.setStatus(2L);
        return toAjax(ab01Service.updateByBo(ab01Bo));
    }
    /**
     * 单位数据强制驳回
     */
    @SaCheckPermission("yuanshidian:ab01:turndown")
    @Log(title = "单位数据强制驳回接口", businessType = BusinessType.UPDATE)
    @GetMapping("/turndown/{ID}")
    public R<Void> turndown(@NotNull(message = "主键不能为空") @PathVariable Long ID ) {
        Ab01Bo ab01Bo = new Ab01Bo();
        ab01Bo.setId(ID);
        ab01Bo.setStatus(1L);
        return toAjax(ab01Service.updateByBo(ab01Bo));
    }

    /**
     * 单位数据审批
     */
    @SaCheckPermission("yuanshidian:ab01:examine")
    @Log(title = "单位数据审批", businessType = BusinessType.UPDATE)
    @PostMapping("/examine")
    public R<Void> examine(@Validated(EditGroup.class) @RequestBody ExamineDataBo examineDataBo) {
        Ab01Bo ab01Bo = new Ab01Bo();
        ab01Bo.setId(examineDataBo.getExaminedataId());
        if(examineDataBo.getExamineStatus()==1){
            ab01Bo.setStatus(3L);
            examineDataBo.setExamineName(LoginHelper.getUsername());
            examineDataBo.setExamineDate(new Date());
            examineDataService.insertByBo(examineDataBo);
            return toAjax(ab01Service.updateByBo(ab01Bo));
        }else{
            ab01Bo.setStatus(4L);
            examineDataBo.setExamineName(LoginHelper.getUsername());
            examineDataBo.setExamineDate(new Date());
            examineDataService.insertByBo(examineDataBo);
            ab01Service.updateByBo(ab01Bo);
            return R.fail(500,"审核数据不通过被驳回");
        }
    }

    /**
     *附件上传
     */
    @SaCheckPermission("yuanshidian:ab01:ab01upload")
    @RepeatSubmit
    @Log(title = "附件上传", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/ab01upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> ab01upload(@RequestParam("file") MultipartFile file, @RequestParam("ID") Long ID) throws Exception {
        if (!file.isEmpty()) {
            String extension = FileUtil.extName(file.getOriginalFilename());
            if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION)) {
                return R.fail("文件格式不正确，请上传" + Arrays.toString(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION) + "格式");
            } else {
                SysOssVo oss = ossService.upload(file);
                Ab01Bo ab01Bo = new Ab01Bo();
                ab01Bo.setId(ID);
                ab01Bo.setFileurl(oss.getUrl());
                return toAjax(ab01Service.updateByBo(ab01Bo));
            }
        }
        return R.fail("上传文件异常，请重新上传");
    }
}
